﻿#include "procreatedlg.h"
#include "ui_procreatedlg.h"
#include <QSettings>
#include <QTextCodec>
#include <QMessageBox>
#include <QDebug>
#include <QFile>

ProCreateDlg::ProCreateDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProCreateDlg)
{
    ui->setupUi(this);
}

ProCreateDlg::~ProCreateDlg()
{
    delete ui;
}

void ProCreateDlg::on_okBt_clicked()
{
    QString fileName = ui->lineEdit_name->text();
    if (fileName.isEmpty()){
        return;
    }
    //创建配置文件
    QString iniFilePath = "Config/Product/" + fileName + ".ini";
    //    判断指定文件是否存在
    if(QFile::exists(iniFilePath)){
        QMessageBox::information(this, tr("Error"), QString::fromLocal8Bit("已有当前文件命名！"));
        return;
    }
    //写入数据
    WdProductIni(iniFilePath);
}




void ProCreateDlg::WdProductIni(QString iniFilePath)
{
    QSettings iniWrite(iniFilePath, QSettings::IniFormat);
    iniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));

    // 设置 信息

    iniWrite.setValue("Type/name", ui->lineEdit_name->text());
    iniWrite.setValue("Type/type", ui->comboBox_type->currentIndex());
    iniWrite.setValue("Type/size", ui->lineEdit_size->text());
    int* nIndex = &m_swAlg.m_ccd1.nCCD;

    // 设置检测开关
    for (int i = 0; i < 20; i++){
        iniWrite.setValue(QString("AlgSwitch/parame%1").arg(i), *nIndex);
        nIndex++;
    }


    // 设置检测参数
    for(int i= 0; i< 4; i++){
        for(int j= 0; j< 22; j++){
            iniWrite.setValue(QString("Defect_%1/parame%2").arg(i+1).arg(j), 0);
        }
        for(int j= 0; j< 19; j++){
            iniWrite.setValue(QString("Rect_%1/parame%2").arg(i+1).arg(j), 0);
        }
        for(int j= 0; j< 19; j++){
            iniWrite.setValue(QString("Trapezoid_%1/parame%2").arg(i+1).arg(j), 0);
        }
    }
    QMessageBox::information(this, tr("Success"), QString::fromLocal8Bit("创建成功！"));
    this->hide();
    emit sigProCreateToMain();
}




void ProCreateDlg::on_noBt_clicked()
{
    this->hide();
}

void ProCreateDlg::on_CCD_GBox1_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd1.nCCD = 1;
    }else{
        m_swAlg.m_ccd1.nCCD = 0;
    }
}

void ProCreateDlg::on_CCD_GBox1_1_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd1.nAlgA = 1;
    }else{
        m_swAlg.m_ccd1.nAlgA = 0;
    }
}

void ProCreateDlg::on_CCD_GBox1_2_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd1.nAlgB = 1;
    }else{
        m_swAlg.m_ccd1.nAlgB = 0;
    }
}

void ProCreateDlg::on_CCD_RBt1_1_1_clicked()
{
    m_swAlg.m_ccd1.nDefect = 1;
}

void ProCreateDlg::on_CCD_RBt1_2_1_clicked()
{
    m_swAlg.m_ccd1.nRect = 1;
}

void ProCreateDlg::on_CCD_RBt1_2_2_clicked()
{
    m_swAlg.m_ccd1.nRect = 2;
}

void ProCreateDlg::on_CCD_GBox2_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd2.nCCD = 1;
    }else{
        m_swAlg.m_ccd2.nCCD = 0;
    }
}

void ProCreateDlg::on_CCD_GBox2_1_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd2.nAlgA = 1;
    }else{
        m_swAlg.m_ccd2.nAlgA = 0;
    }
}

void ProCreateDlg::on_CCD_GBox2_2_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd2.nAlgB = 1;
    }else{
        m_swAlg.m_ccd2.nAlgB = 0;
    }
}

void ProCreateDlg::on_CCD_RBt2_1_1_clicked()
{
    m_swAlg.m_ccd2.nDefect = 1;
}

void ProCreateDlg::on_CCD_RBt2_2_1_clicked()
{
    m_swAlg.m_ccd2.nRect = 1;
}

void ProCreateDlg::on_CCD_RBt2_2_2_clicked()
{
    m_swAlg.m_ccd2.nRect = 2;
}


void ProCreateDlg::on_CCD_GBox3_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd3.nCCD = 1;
    }else{
        m_swAlg.m_ccd3.nCCD = 0;
    }
}

void ProCreateDlg::on_CCD_GBox3_1_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd3.nAlgA = 1;
    }else{
        m_swAlg.m_ccd3.nAlgA = 0;
    }
}

void ProCreateDlg::on_CCD_GBox3_2_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd3.nAlgB = 1;
    }else{
        m_swAlg.m_ccd3.nAlgB = 0;
    }
}

void ProCreateDlg::on_CCD_RBt3_1_1_clicked()
{
    m_swAlg.m_ccd3.nDefect = 1;
}

void ProCreateDlg::on_CCD_RBt3_2_1_clicked()
{
    m_swAlg.m_ccd3.nRect = 1;
}

void ProCreateDlg::on_CCD_RBt3_2_2_clicked()
{
    m_swAlg.m_ccd3.nRect = 2;
}

void ProCreateDlg::on_CCD_GBox4_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd4.nCCD = 1;
    }else{
        m_swAlg.m_ccd4.nCCD = 0;
    }
}

void ProCreateDlg::on_CCD_GBox4_1_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd4.nAlgA = 1;
    }else{
        m_swAlg.m_ccd4.nAlgA = 0;
    }
}

void ProCreateDlg::on_CCD_GBox4_2_clicked(bool checked)
{
    if(checked){
        m_swAlg.m_ccd4.nAlgB = 1;
    }else{
        m_swAlg.m_ccd4.nAlgB = 0;
    }
}

void ProCreateDlg::on_CCD_RBt4_1_1_clicked()
{
    m_swAlg.m_ccd4.nDefect = 1;
}

void ProCreateDlg::on_CCD_RBt4_2_1_clicked()
{
    m_swAlg.m_ccd4.nRect = 1;
}

void ProCreateDlg::on_CCD_RBt4_2_2_clicked()
{
    m_swAlg.m_ccd4.nRect = 2;
}

void ProCreateDlg::on_CCD_RBt1_1_2_clicked()
{
    m_swAlg.m_ccd1.nDefect = 2;
}

void ProCreateDlg::on_CCD_RBt1_1_3_clicked()
{
    m_swAlg.m_ccd1.nDefect = 3;
}

void ProCreateDlg::on_CCD_RBt1_1_4_clicked()
{
    m_swAlg.m_ccd1.nDefect = 4;
}

void ProCreateDlg::on_CCD_RBt2_1_2_clicked()
{
    m_swAlg.m_ccd2.nDefect = 2;
}

void ProCreateDlg::on_CCD_RBt2_1_3_clicked()
{
    m_swAlg.m_ccd2.nDefect = 3;
}

void ProCreateDlg::on_CCD_RBt2_1_4_clicked()
{
    m_swAlg.m_ccd2.nDefect = 4;
}

void ProCreateDlg::on_CCD_RBt3_1_2_clicked()
{
    m_swAlg.m_ccd3.nDefect = 2;
}

void ProCreateDlg::on_CCD_RBt3_1_3_clicked()
{
    m_swAlg.m_ccd3.nDefect = 3;
}

void ProCreateDlg::on_CCD_RBt3_1_4_clicked()
{
    m_swAlg.m_ccd3.nDefect = 4;
}

void ProCreateDlg::on_CCD_RBt4_1_2_clicked()
{
    m_swAlg.m_ccd4.nDefect = 2;
}

void ProCreateDlg::on_CCD_RBt4_1_3_clicked()
{
     m_swAlg.m_ccd4.nDefect = 3;
}

void ProCreateDlg::on_CCD_RBt4_1_4_clicked()
{
     m_swAlg.m_ccd4.nDefect = 4;
}
